﻿using System;
using System.Collections.Generic;
using System.Data.Odbc;
using System.Linq;
using System.Web;

namespace HitachiService.DBAccess
{
    public class ResourceAccess : DataAccess
    {
        public static ResourceData GetResourceById(int id)
        {
            String sql =
                "select " +
                "gid,parent_id,name,username,password,login_enable " +
                "from t_resource " +
                "where gid=" + id;
            ResourceData result = null;
            OdbcConnection conn = GetConnection();
            OdbcCommand cmd = new OdbcCommand(sql, conn);
            conn.Open();
            OdbcDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ResourceData resource = new ResourceData();
                resource.ID = ReadInt(reader["gid"]);
                resource.ParentID = ReadInt(reader["parent_id"]);
                resource.Name = ReadString(reader["name"]);
                resource.Username = ReadString(reader["username"]);
                resource.Password = ReadString(reader["password"]);
                resource.Access = ReadInt(reader["login_enable"]);
                result = resource;
                break;
            }
            conn.Close();
            return result;
        }

        public static List<ResourceData> GetResourcesByParentId(int parent_id)
        {
            String parent_id_str = parent_id == 0 ? " is NULL" : "=" + parent_id;
            String sql =
                "select " +
                "gid,parent_id,name,username,password,login_enable " +
                "from t_resource " +
                "where parent_id" + parent_id_str;
            List<ResourceData> result = new List<ResourceData>();
            OdbcConnection conn = GetConnection();
            OdbcCommand cmd = new OdbcCommand(sql, conn);
            conn.Open();
            OdbcDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ResourceData resource = new ResourceData();
                resource.ID = ReadInt(reader["gid"]);
                resource.ParentID = ReadInt(reader["parent_id"]);
                resource.Name = ReadString(reader["name"]);
                resource.Username = ReadString(reader["username"]);
                resource.Password = ReadString(reader["password"]);
                resource.Access = ReadInt(reader["login_enable"]);
                result.Add(resource);
                break;
            }
            conn.Close();
            return result;
        }

        public static ResourceData GetResourceByUserPswd(String username, String password)
        {
            String sql =
                "select " +
                "gid,resource_level,parent_id,name,username,password,login_enable " +
                "from t_resource " +
                "where username='" + username + "' and password='" + password + "'";
            ResourceData result = null;
            OdbcConnection conn = GetConnection();
            OdbcCommand cmd = new OdbcCommand(sql, conn);
            conn.Open();
            OdbcDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ResourceData resource = new ResourceData();
                resource.ID = ReadInt(reader["gid"]);
                resource.Level = ReadInt(reader["resource_level"]);
                resource.ParentID = ReadInt(reader["parent_id"]);
                resource.Name = ReadString(reader["name"]);
                resource.Username = ReadString(reader["username"]);
                resource.Password = ReadString(reader["password"]);
                resource.Access = ReadInt(reader["login_enable"]);
                result = resource;
                break;
            }
            conn.Close();
            return result;
        }
    }
}